add.html{extend name="property/public/insidePageBase" /}
{block name="title"}进行中租赁{/block}
{block name="css"}
{__block__}
<style>
    .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
        height: 34px;
        line-height: 34px;
        padding: 0 8px;
    }
</style>
{/block}
{block name="body"}
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form   layui-form-pane" action="">
            <div class="layui-inline">
                <label class="layui-form-label">按日期筛选</label>
                <div class="layui-input-inline">
                    <div class="layui-input-inline" style="width: 180px;">
                        <input type="text" name="time" id="time" placeholder="起租日期-结束日期" class="layui-input laydate-type-start-end">
                    </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">关键词搜索</label>
                <div class="layui-input-inline">
                    <input type="text" name="keywords" autocomplete="off" class="layui-input" placeholder="请输入关键词搜索信息">
                </div>
            </div>
            <div class="layui-inline">
                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
            </div>
        </form>

        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                {if $addAuth} <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>{/if}
            </div>
        </script>
        <table class="layui-hide" id="propertyList" lay-filter="propertyList"></table>
        <script type="text/html" id="tableBar">
            <div class="layui-btn-group">
                {if $editAuth} <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">编辑</a>{/if}
                {if $detailAuth} <a class="layui-btn layui-btn-sm" lay-event="detail">详情</a>{/if}
                {if $delAuth}<a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>{/if}
            </div>
        </script>
        <script type="text/html" id="status_tpl">
            {{# if(d.status === 1) { }}<span class="layui-badge layui-bg-orange">待审核</span>{{# } }}
            {{# if(d.status === 2) { }}<span class="layui-badge layui-bg-green">进行中</span>{{# } }}
            {{# if(d.status === 3) { }}<span class="layui-badge layui-bg-gray">已结束</span>{{# } }}
            {{# if(d.status === 4) { }}<span class="layui-badge layui-bg-badge">待签署</span>{{# } }}
            {{# if(d.status === 5) { }}<span class="layui-badge layui-bg-black">已作废</span>{{# } }}
            {{# if(d.status === 6) { }}<span class="layui-badge layui-bg-red">结算中</span>{{# } }}
        </script>
    </div>
</div>
{/block}
{block name="js"}
{__block__}
<script>
    layui.use(['form','laydate',  'table'], function () {
        var $ = layui.jquery, form = layui.form, table = layui.table,laydate = layui.laydate;
        laydate.render({elem: '#time'  ,type: 'date',  range: true });
        var tableId = 'propertyList';
        table.render({
            elem: '#'+tableId,
            id:tableId,
            url: '{:url("propertyContractBasicList")}',
            toolbar: '#toolbar',
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'full_room_name', width: 298, title: '房间信息', sort: true},
                {field: 'number', width: 178, title: '租赁编号', sort: true},
                {field: 'username', width: 128, title: '租客信息'},
                {field: 'mobile', width: 128, title: '联系方式', sort: true},
                {field: 'start_date', width: 106, title: '租赁起始', sort: true},
                {field: 'end_date', width: 106, title: '租赁截止', sort: true},
                {field: 'status', width: 102, title: '租赁状态', templet:'#status_tpl', sort: true, align: "center"},
                {field: 'add_time', width: 168,title: '创建时间'}
                {if $editAuth || $detailAuth}
                ,{title: '操作', minWidth: 128, toolbar: '#tableBar', fixed:'right'}
                {/if}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            height: 'full-80'
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {table.reload(tableId, {page: { curr: 1  }, where: data.field}, 'data'); return false; });
        table.on('rowDouble('+tableId+')', function(obj){
            var data = obj.data;
            var url = '{:url("propertyContractBasicDetail")}?contract_id='+data.id;
            parent.layer.open({
                title: data.number +'  租赁详情', type: 2, shade: 0.2, maxmin:true, shadeClose:true, move:false,area: ['1100px', '100%'],offset:  'rt', anim: 2, content: url , btn: false
            });
        });

        table.on('toolbar('+tableId+')', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var url = '{:url("propertyContractBasicAdd")}';
                var index = layer.open({title: '合同录入', type: 2, shade: 0.2,maxmin:true, area: ['1000px', '90%'], content: url, btnAlign: 'c', btn: ['确定', '取消'], yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                layer.close(loadAdd);
                                if (res.code === 0) {
                                    table.reload(tableId); //数据刷新
                                    layer.close(index); //关闭弹层
                                    layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus(obj.config.id),data = checkStatus.data;
                var data = checkStatus.data, id = [];
                for (elem in data) {
                    id.push(data[elem].id);
                }
                if (id.length === 0) {
                    return layer.msg('请选择要删除的数据');
                }
                var url = '{:url("propertyContractBasicDel")}';
                layer.confirm('删除后不可恢复！  是否确认? ', {icon: 3}, function () {
                    var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                    $.post(url, {ids: id}, function (res) {
                        layer.close(loadAdd);
                        if (res.code === 0) {
                            table.reload(tableId); //数据刷新
                            layer.msg(res.msg, {icon: 6, offset: '15px'});
                        } else {
                            layer.msg(res.msg, {icon: 5, offset: '15px'});
                        }
                    });
                });
            }
        });
        //监听表格复选框选择
        table.on('checkbox('+tableId+')', function (obj) {
            console.log(obj)
        });
        table.on('tool('+tableId+')', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var url = '{:url("propertyContractBasicEdit")}?id='+data.id;
                var index = parent.layer.open({
                    title: '编辑 ' + data.number + ' 租赁信息', type: 2,  shade: 0.2, maxmin:true, area:['1000px', '600px'],content: url, btnAlign: 'c', btn: ['确定', '取消'], yes: function (index, layero) {
                        var iframeWindow = parent.window['layui-layer-iframe' + index], submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = parent.layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                parent.layer.close(loadAdd);
                                if (res.code === 0) {
                                    table.reload(tableId); //数据刷新
                                    parent.layer.close(index); //关闭弹层
                                    parent.layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    parent.layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
                return false;
            } else if (obj.event === 'detail') {
                var url = '{:url("propertyContractBasicDetail")}?contract_id='+data.id;
                var index = parent.layer.open({title: data.number +'  租赁详情', type: 2, shade: 0.2, maxmin:true, shadeClose:true, move:false,area: ['1100px', '100%'],offset:  'rt', anim: 2, content: url , btn:false });
                return false;
            } else if (obj.event === 'delete') {
                var url = '{:url("propertyContractBasicDel")}';
                layer.confirm('删除后不可恢复！  是否确认? ', {icon: 3}, function () {
                    var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                    $.post(url, {ids: [data.id]}, function (res) {
                        layer.close(loadAdd);
                        if (res.code === 0) {
                            table.reload(tableId); //数据刷新
                            layer.msg(res.msg, {icon: 6, offset: '15px'});
                        } else {
                            layer.msg(res.msg, {icon: 5, offset: '15px'});
                        }
                    });
                });
            }
        });
    });
</script>
{/block}